package com.kzd.gtgame

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.*
import com.kzd.gtgame.app.GTApplication
import com.kzd.gtgame.app.GT_FORCE_LOGIN
import com.kzd.gtgame.app.SPSConstance
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.main.*
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashActivity : OwnerViewModeActivity<BaseViewModel>() {

    private var mTimeMillis: Long = 500

    override val layoutResId: Int
        get() = 0

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override fun initView() {
        if (SPSConstance.isReadAgree == false) {
            toActivity()
        } else {
            applyPermissions()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == AGREE_REQUEST_CODE && SPSConstance.isReadAgree == true) {
            mTimeMillis = 0
            if ((applicationContext as GTApplication).initSDK()) {
                applyPermissions()
            }
        }
    }

    override fun initViewMode() {
        viewModel.userInstallFinishLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                SPSConstance.isFirstInstall = false
            } else if (response.status == Status.SUCCESS) {
                SPSConstance.isFirstInstall = true
            }
            if (response.status == Status.START) {
                return@observe
            }
            toActivity()
        })
    }

    private fun applyPermissions() {
//        viewModel.userPermissions(this, {
//            if (ActivityCompat.checkSelfPermission(
//                    (this), Manifest.permission.READ_PHONE_STATE
//                ) == PackageManager.PERMISSION_GRANTED
//            ) {
//                if (!PhoneUtils.getIMEI().isNullOrEmpty()) {
//                    SPSConstance.deviceId = EncryptUtils.encryptMD5ToString(PhoneUtils.getIMEI())
//                        .lowercase()
//                }
//            } else {
//                SPSConstance.isFirstInstall = false
//            }
//            if (SPSConstance.isFirstInstall == false) {
//                viewModel.userInstallFinish()
//            } else {
//                toActivity()
//            }
//        }, Manifest.permission.READ_PHONE_STATE)

        if (SPSConstance.isFirstInstall == false) {
            viewModel.userInstallFinish()
        } else {
            toActivity()
        }
    }

    private fun toActivity() {
        viewModel.viewModelScope.launch(Dispatchers.Main) {
            delay(mTimeMillis)
            if (SPSConstance.isReadAgree == false) {
//                toAgreeActivity(activity = this@SplashActivity)
                val intent = Intent(this@SplashActivity,AgreeActivity::class.java)
                startActivityForResult(intent,AGREE_REQUEST_CODE)
                return@launch
            }
            if (!GT_FORCE_LOGIN || checkLogin()) {
                toMainActivity()
            } else {
                toAccessActivity(AccessAction.LOGIN_CHANNEL)
            }
            finish()
        }
    }
}